package com.wf.ew.sell.biz.impl;

import com.wf.ew.common.BaseDao;
import com.wf.ew.common.PageResult;
import com.wf.ew.common.utils.DateUtil;
import com.wf.ew.common.utils.Utils;
import com.wf.ew.sell.model.SellSon;
import com.wf.ew.system.model.User;
import com.wf.ew.common.constant.UserConstant;
import com.wf.ew.sell.biz.SellBiz;
import com.wf.ew.sell.model.Sell;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;
import java.util.List;

/**
 *
 * 数据层实现类
 * @author : Doman
 * @date : 2019-12-09 10:19:20
 * @version ：1.0
 */
@Repository
public class SellBizImpl extends BaseDao implements SellBiz {

	@Override
	public String list(PageResult<T> page, Sell sell, SellSon sellSon, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<Sell> rm = new BeanPropertyRowMapper<Sell>(Sell.class);
		StringBuffer sql = new StringBuffer("select s.id,s.companyId,s.code");
        sql.append(",s.supplier,s.sellTime,s.warehouseName,s.total");
		sql.append(",s.remark,s.state,s.invoice,s.createTime,s.createUser,s.updateTime");
		sql.append(",s.updateUser");
        sql.append(",cmy.name companyName,suser.name createName ");
        sql.append(" from we_sell s ");
        sql.append(" inner join sys_user suser on suser.id=s.createUser ");
        sql.append(" inner join we_company cmy on cmy.id=s.companyId ");
		if(Utils.isNotEmpty(sellSon)) {
			if (Utils.isNotEmpty(sellSon.getSpecification())) {
				sql.append(" left join we_sell_son ss on ss.sellId=s.id ");
			}
		}
        sql.append(" where s.del=0 and cmy.del=0 ");
		if(!loginUser.getId().equals(UserConstant.ADMIN_ID)){
            sql.append(" and s.companyId=:tablecompanyId ");
            map.put("tablecompanyId", loginUser.getCompanyId());
        }

		if(Utils.isNotEmpty(sellSon)) {
			if (Utils.isNotEmpty(sellSon.getSpecification())) {
				sql.append(" and instr(ss.specification,:specification)>0 ");
				map.put("specification", sellSon.getSpecification());
			}
		}
    	if(Utils.isNotEmpty(sell)){
			if(Utils.isNotEmpty(sell.getKeywords())){
				sql.append(" and (instr(s.code,:code)>0 or instr(s.supplier,:code)>0)");
				map.put("code", sell.getKeywords());
			}
			if(Utils.isNotEmpty(sell.getDate1())){
				sell.setDate1(sell.getDate1()+" 00:00:00");
				sql.append(" and s.sellTime>=DATE_FORMAT(:date1,'%Y-%m-%d %H:%i:%s') ");
				map.put("date1", sell.getDate1());
			}
			if(Utils.isNotEmpty(sell.getDate2())){
				sell.setDate2(sell.getDate2()+" 23:59:59");
				sql.append(" and s.sellTime<=DATE_FORMAT(:date2,'%Y-%m-%d %H:%i:%s') ");
				map.put("date2", sell.getDate2());
			}
			if(Utils.isNotEmpty(sell.getState())){
				sql.append(" and s.state=:state ");
				map.put("state", sell.getState());
			}
			if(Utils.isNotEmpty(sell.getInvoice())){
				sql.append(" and s.invoice=:invoice ");
				map.put("invoice", sell.getInvoice());
			}
			if(Utils.isNotEmpty(sell.getSort())&&Utils.isNotEmpty(sell.getOrder())){
				sql.append(" order by "+sell.getSort()+" "+sell.getOrder());
			} else {
				sql.append(" order by s.sellTime desc ");
			}
		}
		return jsonPage(getPageQueryList(page, sql.toString(), map, rm));
	}

	@Override
	public List<Sell> list(Sell sell, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<Sell> rm = new BeanPropertyRowMapper<Sell>(Sell.class);
        StringBuffer sql = new StringBuffer("select s.id,s.companyId,s.code");
        sql.append(",s.supplier,s.sellTime,s.warehouseName,s.total");
		sql.append(",s.remark,s.state,s.invoice,s.createTime,s.createUser,s.updateTime");
		sql.append(",s.updateUser");
        sql.append(",cmy.name companyName,suser.name createName ");
        sql.append(" from we_sell s ");
        sql.append(" inner join sys_user suser on suser.id=s.createUser ");
        sql.append(" inner join we_company cmy on cmy.id=s.companyId ");
        sql.append(" where s.del=0 and cmy.del=0 ");
		if(!loginUser.getId().equals(UserConstant.ADMIN_ID)){
            sql.append(" and s.companyId=:tablecompanyId ");
            map.put("tablecompanyId", loginUser.getCompanyId());
        }
    	if(Utils.isNotEmpty(sell)){
			if(Utils.isNotEmpty(sell.getKeywords())){
				sql.append(" and (instr(s.code,:code)>0 or instr(s.supplier,:code)>0)");
				map.put("code", sell.getKeywords());
			}
			if(Utils.isNotEmpty(sell.getDate1())){
				sql.append(" and s.sellTime>=DATE_FORMAT(:date1,'%Y-%m-%d') ");
				map.put("date1", sell.getDate1());
			}
			if(Utils.isNotEmpty(sell.getDate2())){
				sql.append(" and s.sellTime<=DATE_FORMAT(:date2,'%Y-%m-%d') ");
				map.put("date2", sell.getDate2());
			}
		}
		getMapParameter(sql, map);
		return jdbcTemplate.query(sql.toString(),map, rm);
	}

	@Override
	public Sell getById(Sell sell) {
		RowMapper<Sell> rm = new BeanPropertyRowMapper<Sell>(Sell.class);
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql = new StringBuffer("select s.id,s.companyId,s.code");
        sql.append(",s.supplier,s.sellTime,s.warehouseName,s.total");
		sql.append(",s.remark,s.state,s.invoice,s.createTime,s.createUser,s.updateTime");
		sql.append(",s.updateUser");
        sql.append(",cmy.name companyName,suser.name createName ");
        sql.append(" from we_sell s ");
        sql.append(" inner join sys_user suser on suser.id=s.createUser ");
        sql.append(" inner join we_company cmy on cmy.id=s.companyId ");
        sql.append(" where s.del=0 and cmy.del=0 and s.id=:id ");
		map.put("id", sell.getId());
		getMapParameter(sql, map);
		return jdbcTemplate.queryForObject(sql.toString(), map, rm);
	}

	@Override
	public KeyHolder add(Sell sell, User loginUser) {
		int count = 1;
		KeyHolder keyHolder=new GeneratedKeyHolder();
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("insert into we_sell (");
        sql.append("companyId,code,supplier,sellTime");
		sql.append(",warehouseName,total,remark,state,invoice,createUser) values ( ");
        sql.append(":companyId,:code,:supplier,:sellTime");
		sql.append(",:warehouseName,:total,:remark,:state,:invoice,:createUser)");
        map.put("companyId", loginUser.getCompanyId());
        map.put("code", sell.getCode());
        map.put("supplier", sell.getSupplier());
        map.put("sellTime", sell.getSellTime());
        map.put("warehouseName", sell.getWarehouseName());
        map.put("total", sell.getTotal()+"");
        map.put("remark", sell.getRemark());
        map.put("state", sell.getState());
        map.put("invoice", sell.getInvoice());
		map.put("createUser", loginUser.getId());
		getMapParameter(sql, map);
		sell.setCompanyId(loginUser.getCompanyId());
		sell.setCreateUser(Integer.valueOf(loginUser.getId()));
		SqlParameterSource paramSource=new BeanPropertySqlParameterSource(sell);
		count = jdbcTemplate.update(sql.toString(), paramSource,keyHolder,new String[]{"id"});
		return keyHolder;
	}

	@Override
	public boolean update(Sell sell, User loginUser) {
		int count = 0;
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update we_sell set ");
        sql.append("code=:code,supplier=:supplier,sellTime=:sellTime ");
		sql.append(",warehouseName=:warehouseName,total=:total," +
				"state=:state,invoice=:invoice,remark=:remark ");
		sql.append(",updateTime=now(),updateUser=:updateUser where id=:id ");
        map.put("code", sell.getCode());
        map.put("supplier", sell.getSupplier());
        map.put("sellTime", sell.getSellTime());
        map.put("warehouseName", sell.getWarehouseName());
        map.put("total", sell.getTotal()+"");
        map.put("remark", sell.getRemark());
        map.put("state", sell.getState());
        map.put("invoice", sell.getInvoice());
		map.put("updateUser", loginUser.getId());
		map.put("id", sell.getId());
		getMapParameter(sql, map);
		count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean updateState(Sell sell, User loginUser) {
		int count = 0;
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update we_sell set ");
		if(Utils.isNotEmpty(sell.getState())){
			sql.append(" state=:state, ");
		}
		if(Utils.isNotEmpty(sell.getInvoice())){
			sql.append(" invoice=:invoice, ");
		}
		sql.append("updateTime=now(),updateUser=:updateUser where id=:id ");
		if(Utils.isNotEmpty(sell.getState())){
			map.put("state", sell.getState());
		}
		if(Utils.isNotEmpty(sell.getInvoice())){
			map.put("invoice", sell.getInvoice());
		}
		map.put("updateUser", loginUser.getId());
		map.put("id", sell.getId());
		getMapParameter(sql, map);
		count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean updateTotal(Sell sell, User loginUser) {
		int count = 0;
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update we_sell set ");
		sql.append(" total=:total ");
		sql.append(",updateTime=now(),updateUser=:updateUser where id=:id ");
        map.put("total", sell.getTotal()+"");
		map.put("updateUser", loginUser.getId());
		map.put("id", sell.getId());
		getMapParameter(sql, map);
		count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean delete(Sell sell, User loginUser) {
		int count = 0;
		Map<String, String> map = null;
		if(sell.getIds().length()>0){
			for(String ID : sell.getIds().split(",")){
				map  = new HashMap<String, String>();
				if(ID==null || ID.equals("")){ break; }
				StringBuffer sql =new StringBuffer("update we_sell set del=1" +
						",updateUser=:updateUser, updateTime=now() " 
						+ " where id =:id ");
				map.put("updateUser", loginUser.getId());
				map.put("id", ID);
				getMapParameter(sql, map);
				count = jdbcTemplate.update(sql.toString(),map);
			}
		}
		return count > 0;
	}
}
